package cn.cdiothree.controller;


import cn.cdiothree.DTO.OrderDTO;
import cn.cdiothree.converter.OrderForm2OrderDTOConverter;
import cn.cdiothree.dataobject.MyPageHelper;
import cn.cdiothree.enums.ResultEnum;
import cn.cdiothree.exception.SellException;
import cn.cdiothree.form.OrderForm;
import cn.cdiothree.service.BuyerService;
import cn.cdiothree.service.ProductOrderService;
import cn.cdiothree.util.ResultVOUtil;
import cn.cdiothree.vo.ResultVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.domain.Page;
//import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.awt.print.Pageable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author CumThing
 * @since 2020-02-02
 */
@RestController
@RequestMapping("/product/order")
@Slf4j
public class ProductOrderController {


    @Autowired
    private ProductOrderService productOrderService;

    @Autowired
    private BuyerService buyerService;



    /**
     * 创建订单
     * @param orderForm
     * @param bindingResult
     * @return
     */
    @PostMapping("/create")
    public ResultVO<Map<String,String>> create(@Valid OrderForm orderForm,
                                               BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            log.error("【创建订单】参数不正确，orderForm",orderForm);
            throw new SellException(ResultEnum.PARAM_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }
        OrderDTO orderDTO= OrderForm2OrderDTOConverter.convert(orderForm);
        if (CollectionUtils.isEmpty(orderDTO.getOrderItemList())){
            log.error("【创建订单】购物车不能为空");
            throw new SellException(ResultEnum.CART_EMPTY);
        }
        OrderDTO createResult=productOrderService.create(orderDTO);

        Map<String,String> map=new HashMap<>();
        map.put("orderId",createResult.getOrderId());
       log.info("李四在2020年4月22日创建订单");
        return ResultVOUtil.success(map);

    }

    /**
     * 订单列表
     * @param userId
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list")
    public ResultVO<List<OrderDTO>> list(@RequestParam(value = "userId") Integer userId,
                                         @RequestParam(value = "page",defaultValue = "0") Integer page,
                                         @RequestParam(value = "size",defaultValue = "10") Integer size){
        //校验
        if (StringUtils.isEmpty(userId)){
            log.error("【查询订单列表】userId为空");
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        PageInfo<OrderDTO> orderDTOPage=productOrderService.findList(page,size,userId);
//        List<OrderDTO> orderDTOS=
//        MyPageHelper
        return ResultVOUtil.success(orderDTOPage);
    }

    /**
     * 订单详情
     * @param userId
     * @param orderId
     * @return
     */
    @GetMapping("/detail")
    public ResultVO<OrderDTO> detail(@RequestParam("userId")Integer userId,
                                     @RequestParam("orderId")String orderId){

        OrderDTO orderDTO =buyerService.findOrderOne(userId,orderId);
        return ResultVOUtil.success(orderDTO);
    }

    /**
     * 取消订单
     * @param userId
     * @param orderId
     * @return
     */
    @PostMapping("/cancel")
    public ResultVO<OrderDTO> cancel (@RequestParam("userId")Integer userId,
                                 @RequestParam("orderId")String orderId){
        buyerService.cancelOrder(userId,orderId);
        return ResultVOUtil.success();
    }


}
